﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SZSLSystem.Model;
using SZSLSystem.DBUtility;

namespace SZSLSystem.DAL
{
    /// <summary>
    /// 数据访问层：角色
    /// </summary>
    public partial class Role
    {
        public Role()
        {

        }

        #region  Method

        /// <summary>
        /// 判断城市编号是否存在
        /// </summary>
        /// <param name="Id">城市编号</param>
        /// <returns></returns>
        public bool Exists(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Role");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        ///  返回数据总数
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) as H ");
            strSql.Append(" from Role ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString()));
        }

        /// <summary>
        /// 增加城市
        /// </summary>
        /// <param name="model">城市实体</param>
        /// <returns></returns>
        public int Add(SZSLSystem.Model.Role model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Role(");
            strSql.Append("ID,RoleName,Description,Sort)");
            strSql.Append(" values (");
            strSql.Append("@ID,@RoleName,@Description,@Sort)");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.UniqueIdentifier),
					new SqlParameter("@RoleName", SqlDbType.VarChar,50),
					new SqlParameter("@Description", SqlDbType.VarChar,500),
                    new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.RoleName;
            parameters[2].Value = model.Description;
            parameters[3].Value = model.Sort;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新城市
        /// </summary>
        /// <param name="model">城市实体</param>
        /// <returns></returns>
        public bool Update(SZSLSystem.Model.Role model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Role set ");
            strSql.Append("RoleName=@RoleName,");
            strSql.Append("Description=@Description");
            strSql.Append("Sort=@Sort");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.UniqueIdentifier),
					new SqlParameter("@RoleName", SqlDbType.VarChar,50),
					new SqlParameter("@Description", SqlDbType.VarChar,500),
                    new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.RoleName;
            parameters[2].Value = model.Description;
            parameters[3].Value = model.Sort;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除城市
        /// </summary>
        /// <param name="Id">城市编号</param>
        /// <returns></returns>
        public bool Delete(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete Role ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)
            };
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据城市编号获取数据
        /// </summary>
        /// <param name="Id">城市编号</param>
        /// <returns></returns>
        public SZSLSystem.Model.Role GetModel(Guid Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from Role ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = Id;

            SZSLSystem.Model.Role model = new SZSLSystem.Model.Role();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                model.ID = Guid.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                model.RoleName = ds.Tables[0].Rows[0]["RoleName"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                model.Sort = int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());

                return model;
            }
            else
            {
                return null;
            }
        }

        public SZSLSystem.Model.Role GetModel(string Name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from Role ");
            strSql.Append(" where RoleName=@RoleName");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleName", SqlDbType.VarChar,50)};
            parameters[0].Value = Name;

            SZSLSystem.Model.Role model = new SZSLSystem.Model.Role();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                model.ID = Guid.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                model.RoleName = ds.Tables[0].Rows[0]["RoleName"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                model.Sort = int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得角色数据列表
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" from Role ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得城市前几行数据
        /// </summary>
        /// <param name="Top">行数</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序条件</param>
        /// <returns></returns>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" from Role ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得城市查询分页数据
        /// </summary>
        /// <param name="pageSize">每页数目</param>
        /// <param name="currentPage">当前页数</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="filedOrder">排序条件</param>
        /// <returns></returns>
        public DataSet GetPageList(int pageSize, int currentPage, string strWhere, string filedOrder)
        {
            int topSize = pageSize * currentPage;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top " + pageSize + " * from Role");
            strSql.Append(" where Id not in(select top " + topSize + " Id from Role");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder + ")");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);

            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}
